myApp =
{
		temp : null,
		currentPosition: null,
		cube:null,
		trackedObject:null,
		trackedObjects:null,
		cssObjects:null,
		divTexts:null,
		objInd:null,
		preDist:null,
		taskType:null,
		cssObject:null,
		divEl:null,
		target1:null,
		target2:null,
		helpLeft:null,
		
		onArgonReady : function()	{
			temp = 0;
			preDist = 0;
			helpLeft = 5;
			//	
			currentPosition = new Object();
			checkPoints.loadCheckPointsData();
			objInd = -1;
			myApp.updateObject();
			
			
			map.init();
			compass.initialCanvas();
			compass.updatecompass();
			initialStatus(objInd);
			setColor(30);
			
			//$("#mainFrame").hide();
			ARGON.loadDataset("http://www.prism.gatech.edu/~xxie37/aro/AR_Project.xml");
			ARGON.onRender = myApp.onRender;
			$('#showImg').hide();
			$('#outlinediv').hide();
			$('#showPos').each(function(index, button) {
				button.addEventListener('touchstart', function() {
					 if(helpLeft ==0){
					 	$('#showPos').hide();
					 }
					 if(helpLeft > 0){
						 console.log("show Position!");
						 showPosTimeLeft = 300;
						 helpLeft--;
						 $("#showPosText")[0].innerHTML = "Show My Position  " + helpLeft + " left";
					}
					 //myApp.updateObject();
				});
			});
			
			$('#nextObj').each(function(index, button) {
				button.addEventListener('touchstart', function() {
					 console.log("show Position!");
					 myApp.updateObject();
				});
			});
			$('#showImg').each(function(index, button) {
				button.addEventListener('touchstart', function() {
					 console.log("show Image!");
					 if( $('#outlinediv').is(":hidden")){
						// $('#showImgText').innerHTML = "Hide Target Image"
						 $('#outlinediv').show();
						 myApp.hideMainFrame();
					 }
					 //$("#mainPart").toggle();
					 //myApp.updateObject();
				});
			});
			$('#outlinediv').each(function(index, img) {
				img.addEventListener('touchstart', function() {
					console.log("img");
						console.log("img hid");
						myApp.showMainFrame();
						$('#showImg').show();
						$('#outlinediv').hide();
						
				});
			});
			
			trackedObjects = new Array();
			cssObjects = new Array();
			divTexts = new Array();
		},
		onDataSetLoaded : function(event)
		{
			var dataset, stonesTarget;
			var redCube, redMaterial, redGeometry;

			dataset      = event.dataset;
			target1 = dataset.targets["history"];
			target2 = dataset.targets["library"];
			
			if (target1)
			{				
				for(var i = 0;i<5;i++){
					cp = checkPoints.checkPointsList[i];
					if(cp.taskType == 0 && cp.taskType ==1){
						var tmpdiv = document.createElement('div');
						tmpdiv.id = "cssContent";
						tmpdiv.style.width = "100px";
						tmpdiv.style.height = "100px";
						tmpdiv.style.backgroundColor = "red";
						tmpdiv.style.position = 'absolute';
						tmpdiv.style.fontSize = "16px";
						tmpdiv.innerText = checkPoints.checkPointsList[i+1].objectText;
						divTexts[i] = tmpdiv;
						
						var tmpcss = new THREE.CSSObject(tmpdiv);
						tmpcss.width = 100;
						tmpcss.height = 100;
						tmpcss.position.x = 0.0;
						tmpcss.position.y = 0.0;
						tmpcss.position.z = 20.0;
						tmpcss.visible = false;
						cssObjects[i] = tmpcss;
						
						var tmpTrackObj = new ARGON.TrackedObject();
						tmpTrackObj.name   = cp.trackObjectName;
						tmpTrackObj.autoHideAfterFrames = 1;
						trackedObject.add(tmpcss);
						trackObjects[i] = tmpTrackObj;
					}
				}
				trackObjects[0].setTarget( target1 );
				trackObjects[1].setTarget( target2 );
			}
		},
		checkOrientation: function(){
			var deviceAt = ARGON.deviceAttitude;
			//console.log("start here");
			//console.log(deviceAt[0]);
			var z = new Array();
			z[0] = deviceAt[2];
			z[1] = deviceAt[6];
			z[2] = deviceAt[10];

			if(z[1]>0.9) { 
				$(".map").hide();
				$("#compass").hide();
				$('#showPos').hide();
				//compass.clearcompass();
				//myApp.hideMainFrame();
			}
			else if(z[2]>0.9){ 
				//myApp.showMainFrame();
				if($("#mainPart").is(":hidden")){
					$(".map").show();
					if(helpLeft >0){
						$('#showPos').show();
					}
				}
				else{
					$(".map").hide();	
					$('#showPos').hide();				
				}
				$("#compass").show();
				//compass.showcompass();
			}

		},
		onRender: function(time) {
			//var loc = navigator.geolocation.getCurrentPosition(myApp.showPosition);
			myApp.showPosition();
			myApp.checkOrientation();
			
			map.render();			
			myApp.checkObject();
		},
		checkObject: function(){
			if(trackedObjects[0]!= null){
				var curPos = trackedObjects[objInd].threeObject3D.matrix.decompose()[0];
				if(objInd == 0){
					if(!(curPos.x ==0 &&curPos.y ==0 && curPos.z==0)){
						alert("Congratulation! You arrived at the 1st check point. You next target is XXX");
						myApp.updateObject();					
						console.log("switch");
						console.log("Object Changed");			
					}
				}
				if(objInd == 1){
					if(curPos.x > -10 && curPos.x <10 && curPos.y >10 && curPos.y <30 && curPos.z>550 && curPos.z>570 ){						
						myApp.updateObject();					
						console.log("switch");
						console.log("Object Changed");
						cssObjects[1].position.z = -300;			
					}
					else{
						cssObjects[1].position.z = 3000000;	
					}
				}
				
			}
		},
		showPosition: function()
		{
			var lat   = ARGON.geolocation.getLLA().latitude;
			var lon   = ARGON.geolocation.getLLA().longitude;
			var alt   = ARGON.geolocation.utm.altitude;
			var position = new Object();
			position.latitude = lat;
			position.longitude = lon;
			
			//myApp.currentPosition.latitude =  position.coords.latitude;
			//myApp.currentPosition.longitude = position.coords.longitude;
			var dist = Geolocation.haversine(position,checkPoints.checkPointsList[objInd]);
			preDist ++;
			setColor(dist);
		},
		endRender:function(){
		},
		updateObject: function(){
			  objInd ++;
			  if(objInd == 5){
					$("#header").hide();
					$("#mainPart").hide();
					$("#objectText").hide();
					$("#map").hide();
					$("#compass").hide();
					alert("You Finished All Tasks! Congratulation!");
					ARGON.onRender = myApp.endRender;
					
			  }
			  currentCP = checkPoints.checkPointsList[objInd];
			  taskType = checkPoints.checkPointsList[objInd].taskType;
			 
			  if(objInd<=3){
			  	updateColor(objInd);
			  }			  
			  if(taskType == 0){
			  	  myApp.showMainFrame();
				  ARGON.onRender = myApp.onRender;
			  }
			  else if(taskType == 1){
				  $('#showImg').show();
			  }
			  else if(taskType == 2){
				  myApp.hideMainFrame();
				  runGame.initialization();
				  //ARGON.onRender = runGame.update;
			  }
			  else if(taskType == 3){
				  runGame.endGame();
				  myApp.showMainFrame();
				  ARGON.onRender = myApp.onRender;
			  }
			  else if(taskType == 4){
				  myApp.hideMainFrame();
				  defenseGame.loadModels();
			  }
			  
		  	  var objectText = document.getElementById("objectText");
			  objectText.innerHTML = "Current Object:" + checkPoints.checkPointsList[objInd].objectText;
		},
		arriveObject:function(){
			var lat   = ARGON.geolocation.getLLA().latitude;
			var lon   = ARGON.geolocation.getLLA().longitude;
			var alt   = ARGON.geolocation.utm.altitude;
			var position = new Object();
			position.latitude = lat;
			position.longitude = lon;
			
			//myApp.currentPosition.latitude =  position.coords.latitude;
			//myApp.currentPosition.longitude = position.coords.longitude;
			var dist = Geolocation.haversine(position,checkPoints.checkPointsList[objInd]);
			console.log(dist);
			if(dist < 10){
				return true;
			}
			else{
				return false;
			}
		},
		hideMainFrame:function(){
			//$("#header").hide();
			$("#mainPart").hide();
			$('#showPos').hide();
			$('#showImg').hide();
			//$("#objectText").hide();
			$("#map").hide();
			$("#compass").hide();
		},
		showMainFrame:function(){
			$("#header").show();
			//$("#mainPart").show();
			$('#showPos').show();
			$("#objectText").show();
			$("#map").show();
			$("#compass").show();
		}
};
